package cn.edu.ujn.lab3.web;

import javax.servlet.http.HttpSession;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.mysql.jdbc.StringUtils;

import cn.edu.ujn.lab3.dao.Project;
import cn.edu.ujn.lab3.dao.User;
import cn.edu.ujn.lab3.service.IProjectService;
import cn.edu.ujn.lab3.service.IUserService;
import cn.edu.ujn.lab3.utils.Page;

@Controller
public class ProjectController {

	@Autowired
	private IProjectService projectService;
	
	@Autowired
	private IUserService userService;

	@RequestMapping(value = "/project/getProject.action")
	@ResponseBody
	public Project getProject(Integer id) {
		Project project = projectService.selectByPrimaryKey(id);
		return project;
	}

	/**
	 * 创建项目
	 */
	@RequestMapping(value = "/project/create.action")
	@ResponseBody
	public String projectCreate(Project project, Integer project_id, Integer employee_id, Integer project_commission,
			String project_version, Integer project_state) {
		if(userService.selectByPrimaryKey(employee_id)==null) {
			return "FAIL1";		//错误1：负责员工不存在
		}
		project.setProject_id(project_id);
		project.setemployee_id(employee_id);
		project.setProject_commission(project_commission);
		project.setProject_version(project_version);
		project.setProject_state(project_state);
		// 执行Service层中的创建方法，返回的是受影响的行数
		int rows = projectService.insertSelective(project);
		System.out.println("rows=" + rows);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL2";		//错误2：插入失败
		}
	}

	/**
	 * 项目列表
	 */

	@RequestMapping(value = "/project/list.action")
	public String list(@RequestParam(defaultValue = "1") Integer page, @RequestParam(defaultValue = "10") Integer rows,
			Integer project_id, Integer employee_id, Integer project_commission, String project_version,
			Integer project_state, Model model) {
		// 条件查询所有用户
		Page<Project> projects = projectService.findProjectList(page, rows, project_id, employee_id, project_commission,
				project_version, project_state);
		// 添加参数
		model.addAttribute("page", projects);
		model.addAttribute("project_id", project_id);
		model.addAttribute("employee_id", employee_id);
		model.addAttribute("project_commission", project_commission);
		model.addAttribute("project_version", project_version);
		model.addAttribute("project_state", project_state);
		return "project";
	}

	/**
	 * 删除项目
	 */
	@RequestMapping("/project/delete.action")
	@ResponseBody
	public String projectDelete(Integer id) {
		int rows = projectService.deleteByPrimaryKey(id);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL";
		}
	}

	/**
	 * 更新项目
	 */
	@RequestMapping("/project/update.action")
	@ResponseBody
	public String projectUpdate(Integer project_id,Integer employee_id, Integer project_commission, String project_version,
			Integer project_state) {
		if(userService.selectByPrimaryKey(employee_id)==null) {
			return "FAIL1";		//错误1：负责员工不存在
		}
		Project project = new Project();
		project.setProject_id(project_id);
		project.setemployee_id(employee_id);
		project.setProject_commission(project_commission);
		project.setProject_version(project_version);
		project.setProject_state(project_state);
		int rows = projectService.updateByPrimaryKeySelective(project);
		if (rows > 0) {
			return "OK";
		} else {
			return "FAIL2";  //错误2：更新失败
		}
	}

}
